package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.StoreProjects;
import com.dingreading.cloud.admin.entity.table.StoreProjectsTableDef;
import com.dingreading.cloud.admin.mapper.StoreProjectsMapper;
import com.dingreading.cloud.admin.service.StoreProjectsService;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class StoreProjectsServiceImpl extends BaseServiceImpl<StoreProjectsMapper, StoreProjects, StoreProjectsTableDef> implements StoreProjectsService {

    @Override
    protected StoreProjectsTableDef getTable() {
        return StoreProjectsTableDef.storeProjects;
    }

    @Override
    public Page<StoreProjects> pageList(
            PageUtil pageUtil, String keyword, Integer lessonStatus, Integer freeStatus,
            Integer source, Integer enabled,
            String cooperationType) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.projectName.like(keyword));
        if (null != lessonStatus)
            condition.and(table.lessonStatus.eq(lessonStatus));
        if (null != freeStatus)
            condition.and(table.freeStatus.eq(freeStatus));
        if (null != source)
            condition.and(table.source.eq(source));
        if (null != enabled)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.source.asc(), table.orderNum.asc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public List<StoreProjects> getList(String keyword) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (null != keyword)
            condition.and(table.projectName.like(keyword));
        condition.and(table.source.eq(1))
                .and(table.enabled.eq(1));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public boolean updateEnabled(String uid, Integer enabled, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid))
                .and(table.source.eq(1));

        return UpdateChain.of(StoreProjects.class)
                .set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public int dropBean(String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid))
                .and(table.storeUid.isNull())
                .and(table.source.eq(1));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public boolean haveName(String uid, String productName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.projectName.eq(productName))
                .and(table.storeUid.isNull())
                .and(table.source.eq(1));
        if (StringUtils.isNotBlank(uid))
            condition.and(table.uid.ne(uid));

        return exists(condition);
    }

    @Override
    public StoreProjects getByUid(String uid) {
        return getMapper().selectOneByCondition(table.uid.eq(uid));
    }

    @Override
    public List<StoreProjects> listByUids(List<String> uids) {
        return getMapper().selectListByCondition(table.uid.in(uids));
    }

    @Override
    public int getMaxOrderNum() {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.isNull())
                .and(table.source.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.orderNum))
                .from(table)
                .where(condition);
        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);
        return NepUtil.nullToZero(integer);
    }

    @Override
    public StoreProjects getGtShowOrder(Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderNum.gt(orderNum))
                .and(table.storeUid.isNull())
                .and(table.source.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition).orderBy(table.orderNum.asc());
        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public StoreProjects getLtShowOrder(Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderNum.lt(orderNum))
                .and(table.storeUid.isNull())
                .and(table.source.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition).orderBy(table.orderNum.desc());
        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public boolean updateOrderNum(String uid, Integer orderNum, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.isNull())
                .and(table.source.eq(1))
                .and(table.uid.eq(uid));

        return UpdateChain.of(StoreProjects.class)
                .set(table.orderNum, orderNum)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public List<StoreProjects> listByProjectNameIn(Set<String> projectNameList) {
        QueryCondition condition = table.projectName.in(projectNameList)
                .and(table.source.eq(1))
                .and(table.enabled.eq(1));
        return getMapper().selectListByCondition(condition);
    }

    @Override
    public boolean updateInfoV2(
            String uid, String projectName, String cooperationType, String operationType, String staffUid, String staffName,
            Integer isPay, String subjectUid, String subjectUidName
    ) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.isNull())
                .and(table.source.eq(1))
                .and(table.uid.eq(uid));

        return UpdateChain.of(StoreProjects.class)
                .set(table.projectName, projectName)
                .set(table.cooperationType, cooperationType)
                .set(table.operationType, operationType)
                .set(table.isPay, isPay)
                .set(table.subjectUid, NepUtil.nullToZero(subjectUid))
                .set(table.subjectUidName, NepUtil.nullToZero(subjectUidName))
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public List<StoreProjects> enabledList() {
        QueryCondition condition = table.enabled.eq(1)
                .and(table.source.eq(1));
        return getMapper().selectListByCondition(condition);
    }

}
